Write-Host "检查CodeQL配置..." -ForegroundColor Green
Write-Host "当前工作目录: $PWD" -ForegroundColor Gray

# 检查CodeQL CLI
Write-Host "`n检查CodeQL CLI..." -ForegroundColor Yellow
try {
    $version = codeql --version 2>&1 | Select-Object -First 1
    if ($version) {
        Write-Host "✓ CodeQL CLI已安装: $version" -ForegroundColor Green
    } else {
        Write-Host "✗ CodeQL CLI未安装或不在PATH中" -ForegroundColor Red
    }
} catch {
    Write-Host "✗ CodeQL CLI未安装或不在PATH中" -ForegroundColor Red
    Write-Host "  安装方式:" -ForegroundColor Gray
    Write-Host "  1. 下载: https://github.com/github/codeql-cli-binaries/releases" -ForegroundColor Gray
    Write-Host "  2. 解压到C:\codeql并添加到PATH" -ForegroundColor Gray
}

# 检查数据库
$dbPath = $env:CODEQL_DB_PATH
if (-not $dbPath) {
    # 检查配置文件中的默认路径
    $configFile = "data-service\src\main\resources\application.yml"
    if (Test-Path $configFile) {
        $configContent = Get-Content $configFile -Raw
        if ($configContent -match 'database-path:\s*\$\{CODEQL_DB_PATH:([^}]+)\}') {
            $dbPath = $matches[1]
            Write-Host "`n从配置文件读取数据库路径: $dbPath" -ForegroundColor Gray
        } else {
            $dbPath = ".\codeql-database"
            Write-Host "`n使用默认数据库路径: $dbPath" -ForegroundColor Gray
        }
    } else {
        $dbPath = "C:\code\totalCode_DB\oh_2d_db"
        Write-Host "`n使用默认数据库路径: $dbPath" -ForegroundColor Gray
    }
    Write-Host "  可通过环境变量CODEQL_DB_PATH自定义" -ForegroundColor Gray
} else {
    Write-Host "`n使用环境变量中的数据库路径: $dbPath" -ForegroundColor Gray
}

Write-Host "`n检查CodeQL数据库..." -ForegroundColor Yellow
if (Test-Path $dbPath) {
    Write-Host "✓ 数据库目录存在: $dbPath" -ForegroundColor Green
    
    # 检查数据库是否有效
    try {
        $dbInfo = codeql database info $dbPath 2>&1 | Select-Object -First 10
        if ($dbInfo) {
            Write-Host "`n数据库信息:" -ForegroundColor Cyan
            $dbInfo | ForEach-Object { Write-Host "  $_" -ForegroundColor Gray }
        }
    } catch {
        Write-Host "⚠ 无法读取数据库信息，可能数据库未创建完成或损坏" -ForegroundColor Yellow
    }
    
    # 检查数据库大小
    $dbSize = (Get-ChildItem -Path $dbPath -Recurse -ErrorAction SilentlyContinue | 
               Measure-Object -Property Length -Sum).Sum / 1GB
    Write-Host "`n数据库大小: $([math]::Round($dbSize, 2)) GB" -ForegroundColor Cyan
    
} else {
    Write-Host "✗ 数据库不存在: $dbPath" -ForegroundColor Red
    Write-Host "`n创建数据库命令:" -ForegroundColor Yellow
    Write-Host "  cd OHCode" -ForegroundColor Gray
    Write-Host "  codeql database create ./codeql-database --language=cpp --source-root=." -ForegroundColor Gray
    Write-Host "`n注意: 数据库创建可能需要数小时，请确保有足够的磁盘空间" -ForegroundColor Yellow
}

# 检查查询文件
Write-Host "`n检查查询文件..." -ForegroundColor Yellow
$queriesPath = $env:CODEQL_QUERIES_PATH
if (-not $queriesPath) {
    $queriesPath = ".\codeql-custom-queries-cpp"
}

if (Test-Path $queriesPath) {
    $queryFiles = Get-ChildItem -Path $queriesPath -Filter "*.ql" -Recurse -ErrorAction SilentlyContinue
    if ($queryFiles) {
        Write-Host "✓ 找到 $($queryFiles.Count) 个查询文件" -ForegroundColor Green
        Write-Host "`n查询文件列表:" -ForegroundColor Cyan
        $queryFiles | ForEach-Object { 
            Write-Host "  - $($_.FullName.Replace($PWD.Path, '.'))" -ForegroundColor Gray 
        }
    } else {
        Write-Host "⚠ 查询文件目录存在但无.ql文件: $queriesPath" -ForegroundColor Yellow
    }
} else {
    Write-Host "⚠ 查询文件目录不存在: $queriesPath" -ForegroundColor Yellow
    Write-Host "  可通过环境变量CODEQL_QUERIES_PATH自定义" -ForegroundColor Gray
}

# 检查Data Service配置
Write-Host "`n检查Data Service配置..." -ForegroundColor Yellow
$configFile = "data-service\src\main\resources\application.yml"
if (Test-Path $configFile) {
    $configContent = Get-Content $configFile -Raw
    if ($configContent -match "codeql:") {
        Write-Host "✓ Data Service配置文件包含CodeQL配置" -ForegroundColor Green
    } else {
        Write-Host "⚠ Data Service配置文件缺少CodeQL配置" -ForegroundColor Yellow
    }
} else {
    Write-Host "⚠ 找不到Data Service配置文件: $configFile" -ForegroundColor Yellow
}

Write-Host "`n检查完成！" -ForegroundColor Green

